﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;

namespace AsTekMobile
{
    public partial class frmSerinoEkle : Form
    {
        public int Miktar { get; set; }
        public string UrunBilgisi { get; set; }
        public string[] SeriNolar
        {
            get
            {
                string[] dizi = new string[dt.Rows.Count];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    dizi[i] = dt.Rows[i][0].ToString();
                }
                return dizi;
            }
        }
        DataTable dt = new DataTable();
        public List<string> EvraktakiSerinolar { get { return _evrakSerinolari; } }
        private List<string> _evrakSerinolari = new List<string>();

        public frmSerinoEkle()
        {
            InitializeComponent();
            dt.Columns.Add("Seri No", typeof(string));
            this.Load += new EventHandler(frmSerinoEkle_Load);
        }

        void frmSerinoEkle_Load(object sender, EventArgs e)
        {
            txtStokAdi.Text = UrunBilgisi;
            dataGrid1.VeriKaynagi = dt;
            dataGrid1.Columns[0].Text = "Seri No";
            dataGrid1.Columns[0].Width = 200;
            txtSerino.Focus();
        }

        private void btnTamam_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }

        private void txtSerino_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Ekle();
            }
        }

        private void Ekle()
        {
            if (!string.IsNullOrEmpty(txtSerino.Text) && SeriNolar.Where(x => x == txtSerino.Text).Count() == 0 && EvraktakiSerinolar.Where(x => x == txtSerino.Text).Count() == 0)
            {
                dt.Rows.Add(txtSerino.Text);
                dataGrid1.Refresh();
                txtSerino.Text = "";
                txtSerino.Focus();

                if (dt.Rows.Count == Miktar)
                {
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
            }
            else
            {
                MessageBox.Show("Boş veya aynı serinoyu tekrar giremezsiniz !");
            }
        }

        private void frmSerinoEkle_Activated(object sender, EventArgs e)
        {
            txtSerino.Focus();
        }

        private void btnEkle_Click(object sender, EventArgs e)
        {
            Ekle();
        }

        private void btnCikar_Click(object sender, EventArgs e)
        {
            if (dataGrid1.CurrentRowIndex > -1)
            {
                dt.Rows.RemoveAt(dataGrid1.CurrentRowIndex);
                dataGrid1.Refresh();
                txtSerino.Text = "";
                txtSerino.Focus();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (Sip.Visible)
            {
                Sip.Visible = false;
            }
            else
            {
                Sip.Visible = true;
            }
        }
    }
}